package com.kernal.demo.smartvision;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kernal.smartvision.adapter.VinParseResultAdapter;
import com.kernal.smartvision.ocr.Devcode;
import com.kernal.smartvision.utils.WriteToPCTask;
import com.kernal.smartvision.view.ResultDialog;
import com.kernal.smartvision.view.ResultLayout;
import com.kernal.smartvisionocr.utils.Utills;
import com.kernal.vinparseengine.VinParseInfo;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.HashMap;
import java.util.List;

public class ShowResultView {
    private RelativeLayout result_top_re;
    LinearLayout result_vin_re;
    private ImageButton result_back,result_ok;
    private TextView result_title,result_tv_feedback;
    private ListView result_vin_list;
    private ImageView result_image;
    private ResultLayout result_view;
    private int srcWidth,srcHeight;
    //VIN 码解析类
    private VinParseInfo vpi;
    private VinParseResultAdapter VPRadapter;
    private List<HashMap<String, String>> vinInfo;


    //表示导入识别还是扫描识别，0 扫描识别，1 导入识别。
    private int startType = 0;
    //表示扫描类型，0 vin, 1 手机号
    private int ocrType = 0;
    //显示的图片
    private String resultPic;
    private String uploadPicPath;
    private String RecogResult = "";
    private View Dialogview;
    private EditText editTextTemp;
    private ImageView image;
    Context context;
    public ShowResultView(Context context){
        this.context=context;


    }
    public void showResultlayoutView(int startType,String RecogResult,int ocrType,String resultPic,String uploadPicPath){
        this.startType=startType;
        this.RecogResult=RecogResult;
        this.ocrType=ocrType;
        this.resultPic=resultPic;
        this.uploadPicPath=uploadPicPath;

        ((Activity)context).setContentView(com.kernal.smartvision.R.layout.activity_show_result);
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        srcWidth = displayMetrics.widthPixels;
        srcHeight = displayMetrics.heightPixels;

        initView();
        initEvent();

    }


    private void initEvent(){
        /**
         * 查询vin 具体信息监听事件
         */
        result_view.setResultChangeListener(new ResultLayout.resultChangeListener() {
            @Override
            public void onChange(String result) {
                //也可以在 finish 中监听。
                if (ocrType == 0){
                    vpi = new VinParseInfo(context.getApplicationContext());
                    vinInfo = vpi.getVinParseInfo(Devcode.devcode,result.toUpperCase());
                    if(vinInfo!=null) {
                        VPRadapter = new VinParseResultAdapter(context.getApplicationContext(), vinInfo, srcWidth, srcHeight);
                        result_vin_list.setAdapter(VPRadapter);
                    }
                }
            }

            @Override
            public void onNull() {
            }

            @Override
            public void onFinished(String result) {
            }
            @Override
            public void onBack() {
               // backEvent();
            }
        });
        result_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((SmartvisionMainActivity)context).initView();
            }
        });

        result_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((SmartvisionMainActivity)context).initView();
            }
        });

        //报错上传
        result_tv_feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater inflater = LayoutInflater.from(context.getApplicationContext());
                //root
                Dialogview = inflater.inflate(context.getResources().getIdentifier("dialog_layout","layout",context.getPackageName()), null);
                editTextTemp = (EditText) Dialogview .findViewById(context.getResources().getIdentifier("dialog_editText_result","id",context.getPackageName()));
                image = (ImageView) Dialogview .findViewById(context.getResources().getIdentifier("dialog_imageView_result","id",context.getPackageName()));
                final ResultDialog selfDialog ;
                selfDialog = new ResultDialog(context);
                selfDialog.setTitle("报错提示");
                selfDialog.setImage(uploadPicPath);
                selfDialog.setResult(RecogResult);
                selfDialog.setYesOnclickListener("确定", new ResultDialog.onYesOnclickListener() {
                    @Override
                    public void onYesClick() {
                        if(Utills.CheckInternet(context.getApplicationContext())){
                            String[] httpContent;
                            httpContent =new String[]{resultPic, editTextTemp.getText().toString()};
                            new WriteToPCTask(context).execute(httpContent);
                        }else{
                            Toast.makeText(context, "请检查网络！", Toast.LENGTH_SHORT).show();
                        }
                        selfDialog.dismiss();
                    }
                });
                selfDialog.setNoOnclickListener("取消", new ResultDialog.onNoOnclickListener() {
                    @Override
                    public void onNoClick() {
                        selfDialog.dismiss();
                    }
                });
                selfDialog.show();
            }
        });
    }
    private void initView(){
        result_top_re = (RelativeLayout) ((Activity)context).findViewById(com.kernal.smartvision.R.id.result_top_re);
        result_vin_re = (LinearLayout)  ((Activity)context).findViewById(com.kernal.smartvision.R.id.result_vin_re);
        result_back = (ImageButton)  ((Activity)context).findViewById(com.kernal.smartvision.R.id.result_back);
        result_title = (TextView)  ((Activity)context).findViewById(com.kernal.smartvision.R.id.result_title);
        result_tv_feedback = (TextView) ((Activity)context).findViewById(com.kernal.smartvision.R.id.result_tv_feedback);
        result_ok = (ImageButton) ((Activity)context).findViewById(com.kernal.smartvision.R.id.result_ok);
        result_vin_list = (ListView) ((Activity)context).findViewById(com.kernal.smartvision.R.id.result_vin_list);
        result_image = (ImageView) ((Activity)context).findViewById(com.kernal.smartvision.R.id.result_image);

        if (ocrType == 0){
            result_view = (ResultLayout) ((Activity)context).findViewById(com.kernal.smartvision.R.id.result_vin_view);
        }else {
            result_view = (ResultLayout)((Activity)context). findViewById(com.kernal.smartvision.R.id.result_phone_view);
        }
        result_view.setVisibility(View.VISIBLE);

        if (RecogResult != null && !RecogResult.equals("")){
            result_view.setContent(RecogResult);
        }

        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
                srcWidth, (int) (srcHeight * 0.06));
        layoutParams.topMargin = 0;
        result_top_re.setLayoutParams(layoutParams);

        RelativeLayout.LayoutParams reLayoutParams = new RelativeLayout.LayoutParams(
                (int) (srcWidth * 0.18), (int) (srcHeight * 0.03));
        reLayoutParams.topMargin = (int) (srcHeight * 0.0155);
        reLayoutParams.leftMargin = (int) (srcWidth * 0.02);
        result_back.setLayoutParams(reLayoutParams);

        reLayoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        reLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        result_title.setLayoutParams(reLayoutParams);

        reLayoutParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        reLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        reLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
        reLayoutParams.rightMargin = (int) (srcWidth * 0.06);
        result_tv_feedback.setLayoutParams(reLayoutParams);

        reLayoutParams = new RelativeLayout.LayoutParams((int) (srcWidth * 0.7), (int) (srcHeight * 0.055));

        reLayoutParams.topMargin = (int) (srcHeight * 0.92);
        reLayoutParams.leftMargin = (int) (srcWidth * 0.15);
        result_ok.setLayoutParams(reLayoutParams);

        if (ocrType == 0){
            reLayoutParams = new RelativeLayout.LayoutParams((int) (srcWidth * 0.9), (int) (srcHeight * 0.56));
            reLayoutParams.topMargin = (int) (srcHeight * 0.33);
            reLayoutParams.leftMargin = (int) (srcWidth * 0.05);
            result_vin_list.setLayoutParams(reLayoutParams);
            result_vin_list.setVisibility(View.VISIBLE);
            vpi = new VinParseInfo(context);
            vinInfo = vpi.getVinParseInfo(Devcode.devcode,RecogResult);
            if(vinInfo!=null) {
                VPRadapter = new VinParseResultAdapter(context, vinInfo, srcWidth, srcHeight);
                result_vin_list.setAdapter(VPRadapter);
            }
        }
        reLayoutParams = new RelativeLayout.LayoutParams((int) (srcWidth * 0.9), (int) (srcHeight * 0.22));
        reLayoutParams.topMargin = (int) (srcHeight * 0.08);
        reLayoutParams.leftMargin = (int) (srcWidth * 0.05);
        result_vin_re.setLayoutParams(reLayoutParams);

        LinearLayout.LayoutParams lLayoutParams = new LinearLayout.LayoutParams((int) (srcWidth*0.88), (int) (srcHeight * 0.12));
        lLayoutParams.gravity = Gravity.CENTER;
        lLayoutParams.weight = 1;
        result_image.setLayoutParams(lLayoutParams);

        lLayoutParams = new LinearLayout.LayoutParams((int) (srcWidth*0.88), (int) (srcHeight * 0.10));
        lLayoutParams.gravity = Gravity.CENTER;
        lLayoutParams.weight = 1;
        result_view.setLayoutParams(lLayoutParams);
        if (ocrType == 0){
            result_title.setText(com.kernal.smartvision.R.string.vin_result);
        }else {
            result_title.setText(com.kernal.smartvision.R.string.phone_result);
        }
        ImageLoader.getInstance().displayImage("file:/"+resultPic,result_image);
    }
}
